package cn.timeapi.Applet.Mall.CatSquare.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * @Description: 自定义响应数据结构
 * 				这个类是提供给门户，ios，安卓，微信商城用的
 * 				门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式（类，或者list）
 * 				其他自行处理
 * 				200：表示成功
 * 			    400: 业务错误
 * 			    401： 参数必填
 * 			    402： 参数类型不匹配
 * 			    403： 参数异常
 * 			    404：请求不存在
 * 				500：表示错误，错误信息在msg字段中
 * 				501：bean验证错误，不管多少个错误都以map形式返回
 * 				502：拦截器拦截到用户token出错
 * 				555：异常抛出信息
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> {

    // 响应业务状态
    private Integer code;

    // 响应消息
    private String msg;

    // 响应中的数据
    private T data;

    public static<T> ServerResponse<T> build(Integer status, String msg, T data) {
        return new ServerResponse<T>(status, msg, data);
    }

    public static<T> ServerResponse<T> success(T data) {
        return new ServerResponse(data);
    }

    public static<T> ServerResponse<T> success() {
        return new ServerResponse(null);
    }

    public static<T> ServerResponse<T> success(String msg) {
        return new ServerResponse(200, msg, null);
    }

    public static<T> ServerResponse<T> success(String msg,T data) {
        return new ServerResponse(200, msg, data);
    }

    public static<T> ServerResponse<T> success(int code,String msg) {
        return new ServerResponse(code, msg, null);
    }

    public static<T> ServerResponse<T> success(int code,String msg,T data) {
        return new ServerResponse(200, msg, data);
    }

    public static<T> ServerResponse<T> errorMsg(String msg) {
        return new ServerResponse(500, msg, null);
    }

    public static<T> ServerResponse<T> error() {
        return new ServerResponse(500, "error", null);
    }

    public static<T> ServerResponse<T> error(String msg) {
        return new ServerResponse(500, msg, null);
    }

    public static<T> ServerResponse<T> error(int code,String msg) {
        return new ServerResponse(500, msg, null);
    }

    public static<T> ServerResponse<T> error(int code,String msg,T data) {
        return new ServerResponse(code, msg, data);
    }

    public static<T> ServerResponse<T> error(T data) {
        return new ServerResponse(500, "error", data);
    }
    
    public static<T> ServerResponse<T> errorTokenMsg(String msg) {
        return new ServerResponse(400, msg, null);
    }
    
    public static<T> ServerResponse<T> errorException(String msg) {
        return new ServerResponse(555, msg, null);
    }

    public ServerResponse() {

    }

    public ServerResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ServerResponse(T data) {
        this.code = 200;
        this.msg = "Success";
        this.data = data;
    }

    public Boolean isSuccess() {
        return this.code == 200;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
